package automatic.generate.code;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

import automatic.generate.code.data.BulidAddFiled;
import automatic.generate.code.data.BulidDetailsFiled;
import automatic.generate.code.data.BulidFiled;
import automatic.generate.code.data.BulidListFiled;
import automatic.generate.code.data.BulidSearchFiled;
import automatic.generate.code.data.BulidUpdateFiled;
import automatic.generate.code.model.DataModel;
import automatic.generate.code.model.FilePath;
import automatic.generate.code.model.FilePath.PathKey;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

public class Main {
	Configuration cfg = new Configuration(new Version(2, 3, 0));
	{
		cfg.setClassForTemplateLoading(Main.class, "/");
		cfg.setDefaultEncoding("utf-8");
	}
	private static String modelName = "org.%1s.database.model.mysql.%s.%s";

	public static void main(String[] args) throws Exception {
		// model名称
		String modelName = "ProClientVersion";
		
		// 系统model
		boolean sysModel = false;

		String proName = "misalen-web";
		String functionName = "管理员管理";
		Set<String> strings = new HashSet<>();
		strings.add("Controller");
		strings.add("Repository");
		strings.add("View-List");
		strings.add("View-Add");
//		strings.add("View-Update");
//		strings.add("View-Details");

		File file = new File(System.getProperty("user.dir"));
		String parentPath = file.getParent();
		FormInfo formInfo = new FormInfo();
		formInfo.setParentPath(parentPath);
		formInfo.setProName(proName);
		formInfo.setModelName(modelName);
		formInfo.setFunctionName(functionName);
		formInfo.setFile(strings);
		formInfo.setSysModel(sysModel);
		new Main().dealWith(formInfo);
	}

	private void dealWith(FormInfo formInfo) throws Exception {
		String proName = formInfo.getProName();
		String parentPath = formInfo.getParentPath();
		String simpleName = formInfo.getModelName();
		String functionName = formInfo.getFunctionName();
		Set<String> set = formInfo.getFile();

		FilePath filePath = new FilePath(proName, parentPath, modelName, simpleName, formInfo.isSysModel());
		filePath.bulidPathAndName();

		Class<?> clazz = Class.forName(filePath.get(PathKey.MODEL_NAME));

		Field[] fieldsSuper = clazz.getSuperclass().getDeclaredFields();
		Field[] fields = clazz.getDeclaredFields();

		DataModel data = new DataModel();
		data.setClassSimpleName(clazz.getSimpleName());
		data.setFunctionName(functionName);

		bulidFiledDatas(fields, fieldsSuper, data);
		for (String string : set) {
			if (string.equals("Controller")) {
				save(data, "template/admin-controllers.ftl", filePath.get(PathKey.ADMIN_CONTROLLERS));
			} else if (string.equals("Repository")) {
				save(data, "template/database-repository.ftl", filePath.get(PathKey.DATABASE_REPOSITORY));
			} else if (string.equals("View-List")) {
				save(data, "template/admin-view-list.ftl", filePath.get(PathKey.ADMIN_VIEW_LIST));
			} else if (string.equals("View-Add")) {
				save(data, "template/admin-view-add.ftl", filePath.get(PathKey.ADMIN_VIEW_ADD));
			} else if (string.equals("View-Update")) {
				save(data, "template/admin-view-update.ftl", filePath.get(PathKey.ADMIN_VIEW_UPDATE));
			} else if (string.equals("View-Details")) {
				save(data, "template/admin-view-details.ftl", filePath.get(PathKey.ADMIN_VIEW_DETAILS));
			}
		}

	}

	private void bulidFiledDatas(Field[] arr1, Field[] arr2, DataModel dataModel) {
		Field[] fields = union(arr1, arr2);
		BulidFiled bulidAddFiled = new BulidAddFiled();
		dataModel.setAddFiledDatas(bulidAddFiled.bulidFiledDatas(fields));

		BulidFiled bulidUpdateFiled = new BulidUpdateFiled();
		dataModel.setUpdateFiledDatas(bulidUpdateFiled.bulidFiledDatas(fields));

		BulidFiled bulidSearchFiled = new BulidSearchFiled();
		dataModel.setSearchFiledDatas(bulidSearchFiled.bulidFiledDatas(fields));

		BulidFiled bulidDetailsFiled = new BulidDetailsFiled();
		dataModel.setDetailsFiledDatas(bulidDetailsFiled.bulidFiledDatas(fields));

		BulidFiled bulidListFiled = new BulidListFiled();
		dataModel.setListFiledDatas(bulidListFiled.bulidFiledDatas(fields));
	}

	public Field[] union(Field[] arr1, Field[] arr2) {
		Set<Field> hs = new HashSet<Field>();
		for (Field str : arr1) {
			hs.add(str);
		}
		for (Field str : arr2) {
			hs.add(str);
		}
		Field[] result = {};
		return hs.toArray(result);
	}

	private void save(DataModel data, String templatePath, String savePath) throws Exception {
		Template template = cfg.getTemplate(templatePath);
		FileOutputStream fos = new FileOutputStream(new File(savePath));
		template.process(data, new OutputStreamWriter(fos, "utf-8"));
	}
}
